package com.djhu.newscollector.crawel.imp.wangyi;

import com.djhu.newscollector.core.Holder.JobHolder;
import com.djhu.newscollector.core.dispatch.JobDispatcher;
import com.djhu.newscollector.core.entity.Response;
import com.djhu.newscollector.crawel.core.PageProcess;
import com.djhu.newscollector.crawel.core.Url;
import com.djhu.newscollector.crawel.imp.worker.MongoService;
import com.djhu.newscollector.crawel.imp.worker.NewsInfo;
import com.djhu.newscollector.crawel.imp.worker.NewsResponse;
import com.djhu.newscollector.crawel.imp.worker.WangyiNewsWorker;
import com.djhu.newscollector.crawel.net.DefaultHttpVisitor;
import com.djhu.newscollector.crawel.net.HttpRequest;
import com.djhu.newscollector.crawel.net.HttpVisitor;
import com.djhu.newscollector.crawel.net.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zw on 2018/2/12.
 */
public class PhotoviewProcess implements PageProcess{

    private static Logger logger = LogManager.getLogger(WangyiNewsWorker.class.getName());


    private JobHolder jobHolder;

    private JobDispatcher jobDispatcher;

    private MongoService mongoService;

    @Override
    public Response process(Url url) {
        NewsResponse newsResponse = new NewsResponse();
        if (url instanceof Url){
            HttpRequest request = new HttpRequest();
            request.setUrl(url);
            HttpVisitor httpVisitor = new DefaultHttpVisitor();



            try {
                Page page = httpVisitor.visit(request);

                Document doc = page.doc();

                try {

                    String title = doc.select(" .headline h1").get(0).text();;
                    if (StringUtils.isEmpty(title)){
                        throw new RuntimeException("title is null");
                    }
                    String date = null;
                    String source = null;
                    try {
                        date = doc.select(" .headline span").get(0).text();
                        source = "wangyi";
                    }catch (Exception e){
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
                        date =  dateFormat.format(new Date());
                        source = "wangyi";
                    }

                    String keyword = null;
                    try {
                        Elements keyElement = doc.select(" meta[name=keywords]");
                        keyword = keyElement.get(0).attr("content");
                    }catch (Exception e){
                        keyword = "";
                    }

                    NewsInfo newsInfo = new NewsInfo();
                    newsInfo.setUrl(url.getUrl());
                    newsInfo.setDatasource("wangyi");
                    newsInfo.setKeys(keyword);
                    newsInfo.setTitle(title);
                    newsInfo.setSource(source);
                    newsInfo.setDate(date);
                    mongoService.insert(newsInfo);
                    //System.out.println("title is " + title + " date is " + date + " source is " + source + " datasource is " + "wangyi");
                    logger.info("title is " + title + " date is " + date + " source is " + source + " datasource is " + "wangyi" );
                }catch (Exception e){
                    if (url.getUrl().endsWith("html")){
                        logger.error("parse e" + e);
                        e.printStackTrace();
                    }
                }

                newsResponse.setPage(page);
            } catch (Exception e) {
                e.printStackTrace();
                newsResponse.setException(e);
            }
        }else {
            newsResponse.setException(new RuntimeException("wrong type!!!"));
        }
        return newsResponse;
    }


    public MongoService getMongoService() {
        return mongoService;
    }

    public void setMongoService(MongoService mongoService) {
        this.mongoService = mongoService;
    }
}